package chapter03;

/**
 * 将二维数组中的行列交换
 * 定义数组变量不能声明其长度，只能在new关键字创建数组时指定
 * Created by a on 2017/6/9.
 */
public class Test043 {
    public static void main(String[] args) {
        int[][] arr = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println("行列互调前");
        printArray(arr);
        int[][] arr2 = new int[arr.length][arr.length];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr2[i][j] = arr[j][i];
            }
        }
        System.out.println("行列互调后");
        printArray(arr2);
    }

    public static void printArray(int[][] arr){
        for (int i = 0;i<arr.length;i++){
            for (int j = 0;j<arr.length;j++){
                System.out.print(arr[i][j]+"  ");
            }
            System.out.println();
        }
    }
}
